package cn.ibizlab.odoo.core.odoo_sale.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isale_order_template_line;
import cn.ibizlab.odoo.core.odoo_sale.domain.Sale_order_template_line;
import cn.ibizlab.odoo.core.odoo_sale.filter.Sale_order_template_lineSearchContext;
import cn.ibizlab.odoo.core.odoo_sale.service.ISale_order_template_lineService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[报价单模板行] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Sale_order_template_lineServiceImpl implements ISale_order_template_lineService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isale_order_template_lineClientService sale_order_template_lineClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isale_order_template_line clientModel = sale_order_template_lineClientService.createModel();
        clientModel.setId(id);
        sale_order_template_lineClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Sale_order_template_line> sale_order_template_lines, int batchSize) {

                        //sale_order_template_lineClientService.createBatch(sale_order_template_lines,batchSize) ;

    }

    @Override
    public void updateBatch(List<Sale_order_template_line> sale_order_template_lines, int batchSize) {

        //sale_order_template_lineClientService.updateBatch(sale_order_template_lines,batchSize) ;

    }

    @Override
    public boolean create(Sale_order_template_line sale_order_template_line) {
        boolean bOk = false ;
        Isale_order_template_line clientModel = this.convert2Model(sale_order_template_line,null);
        sale_order_template_lineClientService.create(clientModel) ;
        this.convert2Domain(clientModel,sale_order_template_line);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Sale_order_template_line get(Integer id) {
        Isale_order_template_line clientModel = sale_order_template_lineClientService.createModel();
        clientModel.setId(id);
        sale_order_template_lineClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Sale_order_template_line get(Sale_order_template_line sale_order_template_line) {
        Sale_order_template_line retsale_order_template_line = this.get(sale_order_template_line.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsale_order_template_line.getClass(), sale_order_template_line.getClass(), false);
        copier.copy(retsale_order_template_line, sale_order_template_line, null);
        return sale_order_template_line ;
    }

    @Override
    public boolean update(Sale_order_template_line sale_order_template_line) {
        boolean bOk = false ;
        Isale_order_template_line clientModel = this.convert2Model(sale_order_template_line,null);
        sale_order_template_lineClientService.update(clientModel) ;
        this.convert2Domain(clientModel,sale_order_template_line) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //sale_order_template_lineClientService.removeBatch(sale_order_template_lines) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Sale_order_template_line> searchDefault(Sale_order_template_lineSearchContext context) {
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品查询
     */
    @Override
    public Page<Sale_order_template_line> selectByOdoo_product(Integer id){
        Sale_order_template_lineSearchContext context = new Sale_order_template_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_template_line> selectByOdoo_create(Integer id){
        Sale_order_template_lineSearchContext context = new Sale_order_template_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_template_line> selectByOdoo_write(Integer id){
        Sale_order_template_lineSearchContext context = new Sale_order_template_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据报价单模板查询
     */
    @Override
    public Page<Sale_order_template_line> selectByOdoo_sale_order_template(Integer id){
        Sale_order_template_lineSearchContext context = new Sale_order_template_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位查询
     */
    @Override
    public Page<Sale_order_template_line> selectByOdoo_product_uom(Integer id){
        Sale_order_template_lineSearchContext context = new Sale_order_template_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template_line> list = new ArrayList<Sale_order_template_line>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template_line> clientModelList = sale_order_template_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isale_order_template_line convert2Model(Sale_order_template_line domain , Isale_order_template_line model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sale_order_template_lineClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getProduct_uom_qtyDirtyFlag())
            model.setProduct_uom_qty(domain.getProduct_uom_qty());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDiscountDirtyFlag())
            model.setDiscount(domain.getDiscount());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getPrice_unitDirtyFlag())
            model.setPrice_unit(domain.getPrice_unit());
         if(domain.getDisplay_typeDirtyFlag())
            model.setDisplay_type(domain.getDisplay_type());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getProduct_uom_id_textDirtyFlag())
            model.setProduct_uom_id_text(domain.getProduct_uom_id_text());
         if(domain.getSale_order_template_id_textDirtyFlag())
            model.setSale_order_template_id_text(domain.getSale_order_template_id_text());
         if(domain.getProduct_uom_idDirtyFlag())
            model.setProduct_uom_id(domain.getProduct_uom_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getSale_order_template_idDirtyFlag())
            model.setSale_order_template_id(domain.getSale_order_template_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Sale_order_template_line convert2Domain( Isale_order_template_line model ,Sale_order_template_line domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Sale_order_template_line();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getProduct_uom_qtyDirtyFlag())
            domain.setProduct_uom_qty(model.getProduct_uom_qty());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDiscountDirtyFlag())
            domain.setDiscount(model.getDiscount());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getPrice_unitDirtyFlag())
            domain.setPrice_unit(model.getPrice_unit());
        if(model.getDisplay_typeDirtyFlag())
            domain.setDisplay_type(model.getDisplay_type());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getProduct_uom_id_textDirtyFlag())
            domain.setProduct_uom_id_text(model.getProduct_uom_id_text());
        if(model.getSale_order_template_id_textDirtyFlag())
            domain.setSale_order_template_id_text(model.getSale_order_template_id_text());
        if(model.getProduct_uom_idDirtyFlag())
            domain.setProduct_uom_id(model.getProduct_uom_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getSale_order_template_idDirtyFlag())
            domain.setSale_order_template_id(model.getSale_order_template_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
